home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10063 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  65 lines

  1. Path: waldorf.csc.calpoly.edu!galaxy!ostoll
  2. From: ostoll@galaxy.csc.calpoly.edu (Oliver Stoll)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructor optimization..
  5. Date: 5 Mar 1996 22:24:35 GMT
  6. Organization: Cal Poly Computer Science Dept.
  7. Message-ID: <4hiev3$5dd@waldorf.csc.calpoly.edu>
  8. References: <9602291637.aa17139@paris.ics.uci.edu>
  9. NNTP-Posting-User: ostoll@galaxy.csc.calpoly.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Michael Heiberg (mheiberg@ahi.ICS.UCI.EDU) wrote:
  13. :   When constructing a variable from the return value of a function that
  14. : returns a copy of that same type, the copy constructor will be called,
  15. : but should the return statement copy the answer into the return space,
  16. : and then copy that into the object being constructed, or should the
  17. : return value be copy-constructed directly into the being-constructed
  18. : object?  For example:
  19.  
  20. :   #include <iostream.h>
  21.  
  22. :   class Vector {
  23. :     public:
  24. :       Vector() { cout << "Ctor "; }
  25. :       Vector(Vector& v) { cout << "Copy "; }
  26. :   };
  27.  
  28. :   Vector foo(Vector &v) { return v; }    // calls the copy-ctor
  29.  
  30. :   main() {
  31. :     Vector a;        // ctor
  32. :     Vector b=foo(a);    // copy-ctor
  33. :   }
  34.  
  35. : Output:
  36. :   Ctor Copy
  37.  
  38. : Function foo returns a Vector by copy, calling the copy constructor, but
  39. : Vector b needs to be constructed with that return value.  I have tried
  40. : this out on a Gnu, Borland, and Microsoft compiler, with optimizations
  41. : turned off, and they all do this operation in one step, producing the
  42. : output listed above.  Is this just a very common and trivial optimization,
  43. : or does the language actually specify that the constructed object is
  44. : copy-constructed with v of foo?
  45.  
  46. :   Any thoughts, answers, or references to relevant sections of one of
  47. : Stroustrup's books would be appreciated.  Reply by email please.
  48.  
  49. : Michael Heiberg - mheiberg@ics.uci.edu - http://www.ics.uci.edu/~mheiberg
  50.  
  51.  
  52. If you return an object by value, then you return a copy of the object, and
  53. thus get a copy constructor call to create that copy.
  54.  
  55.  
  56. Oliver
  57.  
  58. --
  59. -------------------------------------------------------------------------------
  60.                         Oliver "Oyl McDoyle" Stoll
  61.  
  62.  Fachhochschule Karlsruhe                       California Polytechnic State
  63.  stol0012@fh-karlsruhe.de                       University (San Luis Obispo)
  64.  http://www.fh-karlsruhe.de/~stol0012           ostoll@galaxy.csc.calpoly.edu
  65.